package Zoo.impl;

import Zoo.entity.VisitorEntity;
import Zoo.service.VisitorService;
import Zoo.util.TableDTO;
import lesson05.utils.JdbcUtils_DBCP;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;

/**
 * 1、用于参观者身份的登录
 * 2、参观者登录后的主界面
 */

//用于参观者身份的登录
public class VisitorServiceImpl implements VisitorService {
    @Override
    public boolean validateVisitor(VisitorEntity visitorEntity) {
        String visitorName = visitorEntity.getVisitorName();
        String idNumber = visitorEntity.getIdNumeber();
        String sql = "SELECT `id_number` FROM `visitors` WHERE `name`=?";
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            //获取数据库连接
            conn = JdbcUtils_DBCP.getConnection();
            if (conn == null) {
                //如果没获取到数据库连接，则返回false
                return false;
            }
            st = conn.prepareStatement(sql);
            st.setString(1, visitorEntity.getVisitorName());
            rs = st.executeQuery();
            while (rs.next()) {
                String pwd = rs.getString(1);
                if (visitorEntity.getIdNumeber().equals(pwd)) {
                    //如果获取的身份证号是相对应的，则返回true
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.release(conn, st, rs);
        }

        //如果获取的用户名和身份证号是不匹配的，则返回false
        return false;
    }

    //参观者登录后的主界面
    @Override
    public TableDTO retrieveVisitor(VisitorEntity visitorEntity) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT a.`name`,a.`kind`,a.`introduction`,`age`,`parkid`, " +
                "zp.`kind`,zp.`introduction`,`area`,`address`,`begin_time`,`finish_time`,`adminid`  " +
                "FROM `animals` AS a " +
                "INNER JOIN `zooparks` AS zp " +
                "ON zp.`id`=a.`parkid`");
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        //查出来的数据存在这里
        TableDTO returnDTO = new TableDTO();


        try {
            conn = com.JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());
            rs = st.executeQuery();
            //存放连接数据库查出来的数据
            returnDTO.setData(fillData(rs));


            return returnDTO;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            com.JdbcUtils_DBCP.release(conn, st, rs);
        }
        return null;
    }

    //从数据库查出来的数据
    private Vector<Vector<Object>> fillData(ResultSet rs) throws SQLException {
        Vector<Vector<Object>> data = new Vector<>();
        while (rs.next()) {
            Vector<Object> oneRecord = new Vector<>();

            String aName = rs.getString("name");
            String aKind = rs.getString("kind");
            String aIntroduction = rs.getString("introduction");
            String age = rs.getString("age");
            String parkid = rs.getString("parkid");
            String zpKind = rs.getString("kind");
            String zpIntroduction = rs.getString("introduction");
            String area = rs.getString("area");
            String address = rs.getString("address");
            Date beginTime = rs.getDate("begin_time");
            Date finishTime = rs.getDate("finish_time");
            String adminId = rs.getString("adminid");

            oneRecord.addElement(aName);
            oneRecord.addElement(aKind);
            oneRecord.addElement(aIntroduction);
            oneRecord.addElement(age);
            oneRecord.addElement(parkid);
            oneRecord.addElement(zpKind);
            oneRecord.addElement(zpIntroduction);
            oneRecord.addElement(area);
            oneRecord.addElement(address);
            oneRecord.addElement(beginTime);
            oneRecord.addElement(finishTime);
            oneRecord.addElement(adminId);

            data.addElement(oneRecord);
        }
        return data;
    }
}
